package com.example.shiro02.controller;

import com.example.shiro02.service.UserServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @Version 1.0
 * @Author:zlf
 * @Date:2020/11/23 - 16:22
 * @Content:
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @RequestMapping("login")
    public String login(String userName, String userPwd,boolean rememberMe) {
        try {
            userService.checkLogin(userName, userPwd,rememberMe);
            System.out.println("登录成功");
            return "index";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码匹配错误");
            return "login";
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("登录失败");
            return "login";
        }

    }

    @RequestMapping("/regist")
    public String regist(String userName, String userPwd) {
        System.out.println("-----注册");

        //注册时对密码进行加密存储
        Md5Hash md5Hash = new Md5Hash(userPwd);
        System.out.println("------>>>"+md5Hash.toString());

        //加盐加密
        int num = new Random().nextInt(90000)+10000;//10000-99999
        Md5Hash md5Hash1 = new Md5Hash(userPwd,num+"");
        System.out.println("盐："+num);
        System.out.println("加盐------>>>"+md5Hash1.toString());

        //加盐加密+多次Hash
        Md5Hash md5Hash3 = new Md5Hash(userPwd,num+"",3);//循环三次
        System.out.println("加盐+多次Hash------>>>"+md5Hash3.toString());

        //另外一种加密方式
        SimpleHash hash = new SimpleHash("md5",userPwd,num+"",3);
        System.out.println("----"+hash);

        return"login";
    }
}
